package com.suray.basic.wcs.plc.enums;

import com.alibaba.fastjson.serializer.JSONSerializable;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.fasterxml.jackson.annotation.JsonValue;

import java.io.IOException;
import java.lang.reflect.Type;

public enum RobotStatus implements JSONSerializable {
    /**
     * 就绪(1)(上线且空闲)
     */
    READY(1, "就绪"),
    /**
     * 指令执行中（2）
     */
    MOVING(2, "动作中"),
    /**
     * 故障(3)
     */
    ERROR(3, "故障"),
    /**
     * 离线（4）
     */
    OFF_LINE(4, "离线"),
    /**
     * 未知状态
     */
    UNKNOWN(-1, "未知");
    private int robotStatus;
    private String description;

    RobotStatus(int robotStatus, String description) {
        this.robotStatus = robotStatus;
        this.description = description;
    }

    public static RobotStatus parseStatus(int status) {
        RobotStatus[] robotStatuses = RobotStatus.values();
        for (RobotStatus robotStatus : robotStatuses) {
            if (robotStatus.robotStatus == status) {
                return robotStatus;
            }
        }
        return UNKNOWN;
    }

    public int getRobotStatus() {
        return robotStatus;
    }

    @JsonValue
    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return description;
    }

    @Override
    public void write(JSONSerializer jsonSerializer, Object arg1, Type arg2, int arg3) throws IOException {
        jsonSerializer.write(this.getDescription());
    }
}
